<?php


namespace App\Http\ServiceBase\Job;


use App\Enum\ClassAmtTransferEnum;

class PfsHeadAddbatchtransferBase
{

    /**
     * 拆分可以发起划拨、划拨失败||划拨中的记录
     *
     * @param $data
     * @param $last
     *
     * @return array
     */
    public static function group($data, $last)
    {
        $group = ['new' => [], 'exist' => []];
        foreach ($data['list'] as $val) {
            if (empty($last[$val->class_id])) {
                $group['new'][] = $val;
                continue;
            }
            $exist = false;
            foreach ($last[$val->class_id] as $v) {
                /*有任意一条记录结果没有划拨成功,则归类为已存在进行中的划拨*/
                if ($v->result !== ClassAmtTransferEnum::RESULT_SUCCESS || $v->pfs_transfer_status !== ClassAmtTransferEnum::PFS_STATUS_S) {
                    $exist = true;
                }
            }
            if ($exist === true) {
                $group['exist'][] = $val;
            } else {
                $group['new'][] = $val;
            }
        }
        $new = [];
        if (!empty($group['new'])) {
            $new = $data;
            $new['list'] = $group['new'];
        }
        $exist = [];
        if (!empty($group['exist'])) {
            $exist = $data;
            $exist['list'] = $group['exist'];
        }
        unset($group);
        return [
            'new'   => $new,
            'exist' => $exist,
        ];
    }


    /**
     * 如果存在划拨处理中的记录,则等待划拨完成后再调用下次划拨
     *
     * @param $last
     *
     * @return bool
     */
    public static function transferIng($last)
    {
        if (empty($last)) {
            return false;
        }
        foreach ($last as $val) {
            foreach ($val as $v) {
                /*有任意一条记录结果没有划拨成功,则返回true*/
                if ($v->result !== ClassAmtTransferEnum::RESULT_SUCCESS || $v->pfs_transfer_status !== ClassAmtTransferEnum::PFS_STATUS_S) {
                    return true;
                }
            }
        }
        return false;
    }

}





/**
 * PfsHeadAddbatchtransferBase.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/15 17:38:00   操作:创建
 **/
